package com.xzl.xep.web.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.xzl.xep.common.springmvc.DateConvertEditor;
import com.xzl.xep.common.utils.CookieUtil;
import com.xzl.xep.pojo.Operator;
import com.xzl.xep.web.interseptor.Config;
import com.xzl.xep.web.interseptor.WebConstants;

public  class BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	@Autowired  
	protected  HttpServletRequest request;
	
	
	@Autowired  
	protected HttpSession session;
	
	protected Operator operator;
	
	protected Long companyId;
	protected String companyName;


	protected HttpServletResponse response;
	protected CookieUtil cookieUtil;

	public Long SYS_LOGIN_TIMEOUT = Config.getLong(WebConstants.CONFIG_SYS_LOGIN_TIMEOUT); 
	

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}


	@ModelAttribute   
    public void setReqAndRes(HttpServletResponse response) {  
        this.response = response;  
        this.cookieUtil = new CookieUtil(request,response);
        this.operator = (Operator) session.getAttribute(WebConstants.SESSION_OPERATOR);
        if(this.operator != null){
        	this.companyId = this.operator.getCompanyId();
        	this.companyName = this.operator.getCompanyName();
        }
        logger.debug( request.getRequestURI().replace(request.getContextPath(), ""));
    } 

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new DateConvertEditor());
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}
	
	
	
	public void setCookieInSession(String name, Object value){

		cookieUtil.setValue(SYS_LOGIN_TIMEOUT, name, value);
		
	}
	
	public void saveCookie(){
		cookieUtil.save();
	}
	
	
}
